package jys.test.yddata.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class DrawHookView extends View {
    private int arc_blue = Color.parseColor("#10a679");
    private int progress = 0;
    private int line1_x = 0;
    private int line1_y = 0;
    private int line2_x = 0;
    private int line2_y = 0;
    private boolean secondline = false;

    public DrawHookView(Context context) {
        super(context);
    }

    public DrawHookView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawHookView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    @SuppressLint("DrawAllocation")
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int speed = 20;
        progress+=speed;

        Paint paint = new Paint();
        //设置画笔颜色
        paint.setColor(arc_blue);
        //设置圆弧的宽度
        paint.setStrokeWidth(30);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.STROKE);
        //消除锯齿
        paint.setAntiAlias(true);

        //获取圆心的x坐标
        int center = getWidth() / 2;
        int center1 = center - getWidth() / 5;
        //圆弧半径
        int radius = getWidth() / 2 - 30;

        //定义的圆弧的形状和大小的界限
        RectF rectF = new RectF(center - radius -1, center - radius -1 ,center + radius + 1, center + radius + 1);

        //根据进度画圆弧
        canvas.drawArc(rectF, 235, -360 * progress / 500, false, paint);

        //先等圆弧画完，才话对勾
        if(progress >= 20) {
            if(line1_x < radius / 3) {
                line1_x+=speed;
                line1_y+=speed;
            }
            //画第一根线
            canvas.drawLine(center1+10, center+10, center1 + line1_x+10, center + line1_y+10, paint);

            if (line1_x >= radius / 3 && line2_x <= radius) {
                if (!secondline) {
                    line1_x+=speed;
                    line1_y+=speed;
                    line2_x = line1_x;
                    line2_y = line1_y;
                    secondline = true;
                }
                else {
                    line2_x+=speed;
                    line2_y-=speed;
                }
            } else if (progress > 1000) {
                progress = 0;
                line1_x = 0;
                line1_y = 0;
                line2_x = 0;
                line2_y = 0;
                secondline = false;
                setVisibility(View.GONE);
            }
            if (secondline) {
                //画第二根线
                canvas.drawLine(center1 + line1_x, center + line1_y, center1 + line2_x, center + line2_y, paint);
            }
        }

        //每隔10毫秒界面刷新
        postInvalidateDelayed(10);
    }
}
